package lz.controls.nick
{
	import flash.display.Shape;
	import flash.display.Sprite;

	/**
	 * 画蜘蛛图 
	 * @author Administrator
	 * 
	 */	
	public class SpiderShape extends Sprite
	{
		/**
		 * 最大长度 
		 */
		private const LINELEN:int = 45;
		/**
		 *最小长度 
		 */
		private const MIN_LINELEN:int = 15;
		/**
		 *最大值 
		 */
		private var  _max_value:int = 120;
		/**
		 * 颜色 
		 */
		private var _color:uint;
		/**
		 *基础值 
		 */
		private var _shapeBase:Shape;
		/**
		 *加成值 
		 */
		private var _additionShape:Shape;
		public function SpiderShape()
		{
			super();
			if(_additionShape == null)_additionShape = new Shape();
			this.addChild(_additionShape);
			if(_shapeBase == null)_shapeBase = new Shape();
			this.addChild(_shapeBase);
		}
		/**
		 * 角度
		 * 
		 */
		public function setData($arr:Array,$color:uint = 0x3333FF,$alpha:Number = 0.5):void
		{
			setAdditionData($arr);
			_color = $color;
			_shapeBase.graphics.clear();
			_shapeBase.graphics.beginFill($color,$alpha);
			var len:int = $arr.length;
			for(var i:int = 0;i < len;i++)
			{
				_shapeBase.graphics.lineTo(calculateX($arr[i][0],$arr[i][1]),calculateY($arr[i][0],$arr[i][1]));
			}
			_shapeBase.graphics.lineTo(calculateX($arr[0][0],$arr[0][1]),calculateY($arr[0][0],$arr[0][1]));
			_shapeBase.graphics.endFill();
		}
		/**
		 * 加成值 
		 * @param $arr
		 * @param $color
		 * @param $alpha,0x33CCFF,0.8
		 * 
		 */
		public function setAdditionData($arr:Array,$color:uint = 0x33CCFF,$alpha:Number = 0.8):void
		{
			_color = $color;
			_additionShape.graphics.clear();
			_additionShape.graphics.beginFill($color,$alpha);
			var len:int = $arr.length;
			for(var i:int = 0;i < len;i++)
			{
				_additionShape.graphics.lineTo(calculateX($arr[i][0],$arr[i][1]),calculateY($arr[i][0],$arr[i][1]));
			}
			_additionShape.graphics.lineTo(calculateX($arr[0][0],$arr[0][1]),calculateY($arr[0][0],$arr[0][1]));
			_additionShape.graphics.endFill();
//			setData($arr);
		}
		/**
		 *更新 
		 * @param $arr
		 * 
		 */
		public function upData($arr:Array):void
		{
			_shapeBase.graphics.clear();
			_shapeBase.graphics.beginFill(_color);
			var len:int = $arr.length;
			for(var i:int = 0;i < len;i++)
			{
				_shapeBase.graphics.lineTo(calculateX($arr[i][0],$arr[i][1]),calculateY($arr[i][0],$arr[i][1]));
			}
			//graphics.lineTo(calculateX($arr[0][0],$arr[0][1]),calculateY($arr[0][0],$arr[0][1]));
			graphics.endFill();
		}
		/**
		 * 设置长度 
		 * @param $value
		 * 
		 */
		public function set lineLen($value:int):void
		{
			_max_value = $value;
		}
		/**
		 *x坐标  
		 * @param $len(长度
		 * @param $radians（角度）
		 * @return 
		 * 
		 */		
		private function calculateX($len:int,$radians:int):Number
		{
			var len:int = $len;
			len = (LINELEN - MIN_LINELEN)* $len/ _max_value + MIN_LINELEN;
			if(len == LINELEN)
				len = LINELEN -2;
			var x:int = Math.floor(len * Math.cos($radians*Math.PI / 180));
			return x;
		}
		/**
		 *y坐标 
		 * 
		 */
		private function calculateY($len:int,$radians:int):Number
		{
			var len:int = $len;
			len = (LINELEN - MIN_LINELEN)* $len/ _max_value + MIN_LINELEN;
			if(len == LINELEN)
				len = LINELEN -2;
			var y:int = Math.floor(len * Math.sin($radians*Math.PI / 180));
			return -y;
		}
		/**
		 *重置 
		 * 
		 */
		public function reset():void
		{
			_shapeBase.graphics.clear();
			_additionShape.graphics.clear();
		}
		
	}
}